/// @author: cairuoyu
/// @homepage: http://cairuoyu.com
/// @github: https://github.com/cairuoyu/flutter_admin
/// @date: 2021/6/21
/// @version: 1.0
/// @description:

import 'package:flutter_admin/pages/common/cry/cry_transfer.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/base_stationpath_odoo_api.dart';
import 'package:flutter_admin/api/odoo/base_stationuser_odoo_api.dart';
import 'package:flutter_admin/api/role_user_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/base_productionstation.dart';
import 'package:flutter_admin/models/odoo/base_stationpath.dart';
import 'package:flutter_admin/models/odoo/base_stationuser.dart';
import 'package:flutter_admin/models/role.dart';
import 'package:flutter_admin/models/role_user.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/odoo/base_productionstation/base_stationpath/station_path_select_list.dart';
import 'package:flutter_admin/pages/role/role_user_select_list.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';


class StationPathSelect extends StatefulWidget {
  StationPathSelect({Key? key, this.baseProductionstation}) : super(key: key);
  final BaseProductionstation? baseProductionstation;

  @override
  _StationPathSelectState createState() => _StationPathSelectState();
}

class _StationPathSelectState extends State<StationPathSelect> {
  final GlobalKey<StationPathSelectListState> tableKey1 =
      GlobalKey<StationPathSelectListState>();
  final GlobalKey<StationPathSelectListState> tableKey2 =
      GlobalKey<StationPathSelectListState>();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var table1 = StationPathSelectList(
        key: tableKey1,
        title: '未配置工序',
        baseProductionstation: widget.baseProductionstation);
    var table2 = StationPathSelectList(
        key: tableKey2,
        title: '已配置工序',
        baseProductionstation: widget.baseProductionstation,
        isSelected: true);
    var transfer = CryTransfer(
      left: table1,
      right: table2,
      toRight: () async {
        List<BaseStationPath> selectedList =
            tableKey1.currentState!.getSelectedList();
        if (selectedList.isEmpty) {
          CryUtils.message("请选择工序");
          return;
        }
        List stationpathList = selectedList
            .map((e) => BaseStationPath(
                    processId: e.processId,
                    stationId: widget.baseProductionstation!.id)
                .toOdooMap())
            .toList();
        await BaseStationpathOdooApi.saveOrUpdate(stationpathList).then((ResponseBodyApi res){
          if(!res.success!){
            CryUtils.message(res.message!);
            return;
          }
          CryUtils.message(S.of(context).saved);
          tableKey1.currentState!.query();
          tableKey2.currentState!.query();
        });
      },
      toLeft: () async {
        List<BaseStationPath> selectedList =
            tableKey2.currentState!.getSelectedList();
        if (selectedList.isEmpty) {
          CryUtils.message("请选择工序");
          return;
        }
        List ids = selectedList.map((v) {
          return int.parse(v.id!);
        }).toList();
        await BaseStationpathOdooApi.removeByIds(ids).then((ResponseBodyApi res){
          if(!res.success!){
            CryUtils.message(res.message!);
            return;
          }
          CryUtils.message(S.of(context).saved);
          tableKey1.currentState!.query();
          tableKey2.currentState!.query();
        });
      },
    );

    var result = Scaffold(
      appBar: AppBar(
        leading: Builder(
          builder: (BuildContext context) {
            final ScaffoldState? scaffold = Scaffold
                .maybeOf(context);
            final ModalRoute<Object?>? parentRoute = ModalRoute.of(context);
            final bool canPop = parentRoute?.canPop ?? false;
            if (canPop) {
              return BackButton();
            } else {
              return SizedBox.shrink();
            }
          },
        ),
        title: Text('车间工位与工序配置'),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [transfer],
        ),
      ),
    );
    return result;
  }
}
